﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using AcctruePMS.Bll;
using AcctruePMS.Bll.Manager;
using System.Data;
using AcctruePMS.Bll.Common;
namespace AcctrueWMS.Web
{
    public partial class ExportExcel : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string type = Request.Form["FileType"];
            switch (type)
            {
                case "ExportByTemplete":
                    ExportExcelByTemplete();
                    break;
                default:
                    ExportExcelByHtml();
                    break;
            }
          
        }   
        public void ExportExcelByHtml() {

            string exportvalue = Request["exportvalue"];
            string FileName = Request["filename"];
            exportvalue = Microsoft.JScript.GlobalObject.unescape(Server.HtmlDecode(exportvalue));
            exportvalue = exportvalue.Replace("?", "");
            //exportvalue = exportvalue.Replace(" ", "");

            exportvalue = exportvalue.Replace("<TBODY>", "");
            exportvalue = exportvalue.Replace("</TBODY>", "");
            exportvalue = exportvalue.Replace("<TABLE", "<TABLE border=\"1\" bordercolor=\"#000000\"");
            exportvalue = exportvalue.Replace("<table", "<table border=\"1\" ");
            exportvalue = exportvalue.Replace("border=0", "border=1 ");
            exportvalue = exportvalue.Replace("border='0'", "border=1 ");
            exportvalue = exportvalue.Replace("border=\"0\"", "border=1 ");
            exportvalue = exportvalue.Replace("STYLE", "stylefrom ");
            exportvalue = exportvalue.Replace("<TD><SPAN></SPAN></TD>", "");
            exportvalue = exportvalue.Replace("创建人", "姓名");
            exportvalue = exportvalue.Replace("<td>", " <td style=\"vnd.ms-excel.numberformat:@\">");
            exportvalue = exportvalue.Replace("<TD>", " <td style=\"vnd.ms-excel.numberformat:@\">");
            exportvalue = exportvalue.Replace("<A ", "<SPAN ");
            exportvalue = exportvalue.Replace("</A>", "</SPAN>");
            exportvalue = exportvalue.Replace("<IMG ", "<IMG1 ");
            exportvalue = exportvalue.Replace("type=image ", "type=image1 ");
            exportvalue = exportvalue.Replace("<TH>", "<TH style=\"height: 20px;font: 12px; font-weight: bold;vertical-align: middle; text-align: center; border-left: 1px solid #000000; white-space: nowrap; background-color: #d8efFF; height: 20px;font-size: 12px;color: #000000;\">");
            exportvalue = exportvalue.Replace("id=tabDivContral", "style=\" background-color:#95B0CB\"");
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-excel";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName + "_" + string.Format("{0:d}", System.DateTime.Now) + ".xls", System.Text.Encoding.UTF8).ToString());
            StringWriter sw = new StringWriter();
            sw.WriteLine(exportvalue);
            HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);
            Control cr = new Control();
            cr.RenderControl(htmlWriter);
            //强制输出bom 这样避免excel打开时乱码  
            Response.BinaryWrite(new byte[] { 0xEF, 0xBB, 0xBF });
            Response.Write(sw.ToString());
            Response.End();
        
        }
        /// <summary>
        /// 根据Excel模板导入数据
        /// </summary>
        public void ExportExcelByTemplete() {
            if (Request.Form["reportType"]=="weekReport")
            {
                ExportWeekReportExcel();
            }
            else if (Request.Form["reportType"] == "programState")
            {
                ExportProgramStateExcel();
            }
           
        
        }
        /// <summary>
        /// 导出周报
        /// </summary>
        public void ExportWeekReportExcel() {
            WeekReportManager manager = new WeekReportManager();
            string department = Request.Form["departement"];
            string userNo = Request.Form["userNo"];
            string reportStatus = Request.Form["reportStatus"];
            string startDate1 = Request.Form["startDate1"];
            string endDate1 = Request.Form["endDate1"];
            string startDate2 = Request.Form["startDate2"];
            string endDate2 = Request.Form["endDate2"];
            string currentUserNo = Session["userNo"].ToString();
            string path = AppDomain.CurrentDomain.BaseDirectory + Request.Form["path"].ToString();
            DataTable dt = manager.GetWeekReportByCondition(currentUserNo, userNo, department, reportStatus, startDate1, endDate1, startDate2, endDate2);
            MemoryStream ms = ExcelHelper.GetExcelWork(dt, path);
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-excel";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("周报" + "_" + string.Format("{0:d}", System.DateTime.Now) + ".xls", System.Text.Encoding.UTF8).ToString());


            Response.BinaryWrite(ms.GetBuffer());
            Response.End();
        }
        /// <summary>
        /// 导出项目状态
        /// </summary>
        public void ExportProgramStateExcel() {
            ProjectManager pm = new ProjectManager();
            string projectName = Request.Form["projectName"];
            string departWBS = Request.Form["departWBS"];
            string projectManager = Request.Form["projectManager"];
            string status = Request.Form["status"];
            string contactCode = Request.Form["contactCode"];
            string customName = Request.Form["customName"];
            string projectType = Request.Form["projectType"];
            string path = AppDomain.CurrentDomain.BaseDirectory + Request.Form["path"].ToString();
            string reportType = Request.Form["reportType"];
            string contractTime1 = Request.Form["contractTime1"];
            string contractTime2 = Request.Form["contractTime2"];
            DataTable dt = pm.GetProject(projectName, departWBS, projectManager, status, contactCode, customName, projectType, contractTime1, contractTime2);
            MemoryStream ms = ExcelHelper.GerneralExcelExport(dt, path);
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-excel";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("项目" + "_" + string.Format("{0:d}", System.DateTime.Now) + ".xls", System.Text.Encoding.UTF8).ToString());


            Response.BinaryWrite(ms.GetBuffer());
            Response.End();
        }

    }
}